<template>
    <div class="device-list">
        <div class="device-list-container flex-container">


            <div class="device-list-table">

                <el-table :data="userList" border>

                    <el-table-column label="ID" prop="id" width="60"></el-table-column>
                    <el-table-column label="微信昵称" prop="nickname" min-width="80"></el-table-column>
                    <el-table-column label="头像" min-width="60">
                        <template slot-scope="scope">
                            <img :src="scope.row.avatar | imgUrl" alt="" class="thumb-img" v-viewer>
                        </template>
                    </el-table-column>
                    <el-table-column label="注册时间">
                        <template slot-scope="scope">
                            {{scope.row.create_time | timeFormat}}
                        </template>
                    </el-table-column>
                    <el-table-column label="已分配的医生" width="60">
                        <template slot-scope="scope">
                            {{scope.row.user_track && scope.row.user_track.doctor_name || '--' }}
                        </template>
                    </el-table-column>
                    <el-table-column label="VIP信息" width="200">
                        <template slot-scope="scope">
                            <template v-if="scope.row.user_vip">
                                <div>会员等级: {{scope.row.user_vip.vip_name}}</div>
                                <div>开始时间: {{scope.row.user_vip.begin_time | timeFormat}}</div>
                                <div>结束时间: {{scope.row.user_vip.end_time | timeFormat}}</div>
                                <div>剩余天数: {{scope.row.user_vip.remain_days}}天</div>
                            </template>
                            <template v-else>未开通</template>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="200">
                        <template slot-scope="scope">
                            <el-button size="mini" type="primary outline"
                                       @click="showAssignModal(scope.row)">
                                分配医生
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

            <div class="flex-footer h-80">
                <div class="flex-footer-content">
                    <el-row>
                        <el-col :span="8" class="pt-6">
                            &nbsp;
                            <!--<el-button type="success" @click="handleAddUser">添加用户</el-button>-->
                        </el-col>
                        <el-col :span="16" class="text-right" v-if="total > 0">
                            <el-pagination
                                    @current-change="pageChange"
                                    :current-page="currentPage"
                                    :page-size="pageSize"
                                    layout="total, prev, pager, next, jumper"
                                    :total="total">
                            </el-pagination>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>

        <!--编辑弹窗-->
        <el-dialog title="医生分配" custom-class="edit-dialog" :visible.sync="editModalVisible"
        @close="handleModalClose">
            <el-form :label-position="'left'" label-width="110px" ref="userForm" :model="userForm">
                <el-form-item label="医院">
                    <el-select v-model="userForm.sp" filterable placeholder="请选择" @change="handleSpChange()">
                        <el-option
                                v-for="item in hospitalList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="医生">
                    <el-select v-model="userForm.doctorId" filterable placeholder="请选择">
                        <el-option
                                v-for="item in doctorList"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="success" @click="saveUser('userForm')">确认</el-button>
            </div>
        </el-dialog>


    </div>
</template>

<script type="text/ecmascript-6">

    import Core from 'core/core'

    export default {
        data() {
            return {
                editModalVisible: false,
                userForm: {
                    sp: "",
                    doctorId: "",
                    userId: 0
                },
                userList: [],
                doctorList: [],
                hospitalList: [],

                // 分页配置
                currentPage: 1,
                total: 0,
                pageSize: 10,
            }
        },
        mounted: function () {
            this._userList();
            this._hospitalList();
            this._doctorList();
        },

        methods: {
            _userList() {
                Core.Api.User.list(1, 0, this.currentPage)
                    .then(res => {
                        this.userList = res.list;
                        this.total = res.count
                    })
                    .catch(err => {})
            },

            _doctorList() {
                Core.Api.User.list(2, this.userForm.sp, 1, 60)
                    .then(res => {
                        this.doctorList = res.list;
                    })
                    .catch(err => {})
            },

            _hospitalList() {
                Core.Api.Hospital.list(this.currentPage, 50)
                    .then(res => {
                        this.hospitalList = res.list;

                    })
                    .catch(err => {
                    })
            },

            pageChange(val) {
                this.currentPage = val;
                this._userList();
            },

            saveUser(formName) {
                Core.Api.User.assignDoctor(this.userForm.userId, this.userForm.doctorId)
                    .then(res => {
                        this.editModalVisible = false;
                        this.$message.success("分配成功");
                        this._userList();
                    })
            },

            showAssignModal(user) {
                this.userForm.userId = user.id;
                if (user.user_track) {
                    this.userForm.sp = user.user_track.hospital_id;
                    this.userForm.doctorId = user.user_track.doctor_id;
                }
                this.editModalVisible = true;
            },

            handleModalClose() {
                this.userForm = {
                    doctorId: "",
                        userId: 0
                };
            },
            handleSpChange() {
                this._doctorList();
            }

        }
    }
</script>

<style scoped lang="scss">
    .device-list {
        height: 100%;
        .device-list-container {
            height: 100%;
            .search-row {
                margin: 0;
                padding: 15px 30px;
                background: #fff;
                .el-form--inline .el-form-item {
                    margin-bottom: 0;
                }
                .el-form-item__label {
                    padding-right: 10px;
                    font-size: 12px;
                }
                .el-form-item__content {
                    .el-input {
                        width: 175px;
                    }
                }
            }

            .device-list-table {
                padding: 20px 0;
                background: #ffffff;
                position: relative;
                flex: 1;
                overflow: auto;
                .all-select-holder {
                    line-height: 30px;
                    position: absolute;
                    left: 10px;
                    z-index: 10;
                }
            }
            .flex-footer {
                border-top: 1px solid #E0E7EF;
            }
            .flex-footer.h-80 {
                height: 80px;
            }

        }

        a.success {
            display: inline-block;
            color: #fff;
            background-color: #03CC75;
            border-color: #03CC75;
            line-height: 1;
            white-space: nowrap;
            cursor: pointer;
            -webkit-appearance: none;
            text-align: center;
            box-sizing: border-box;
            outline: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
            padding: 7px 16px;
            font-size: 12px;
            border-radius: 4px;
            margin: 0 10px;
        }

        .upload-device {
            display: inline-block;
        }
    }
</style>
